package com.example.springbootkafkademo;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author chaird
 * @create 2021-09-21 18:49
 */
@Component
public class KafkaConsumer {
  @KafkaListener(topics = {"ods_ecodata"})
  public void consumer(ConsumerRecord<?, ?> consumerRecord) {
    // 判断是否为null
    Optional<?> kafkaMessage = Optional.ofNullable(consumerRecord.value());
    if (kafkaMessage.isPresent()) {
      // 得到Optional实例中的值
      Object message = kafkaMessage.get();
      System.err.println("消费消息:" + message);
    }
  }
}
